<!-- 编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="750"
    :model-value="modelValue"
    :title="isUpdate ? '修改地区' : '添加地区'"
    @update:modelValue="updateModelValue"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="地区名称" prop="name">
            <el-input
              clearable
              v-model="form.name"
              placeholder="请输入地区名称"
            />
          </el-form-item>
          <el-form-item label="地区代码" prop="code">
            <el-input
              clearable
              v-model="form.code"
              placeholder="请输入地区代码"
            />
          </el-form-item>
          <el-form-item label="扩展填充" prop="isNeedExtend">
            <el-radio-group v-model="form.isNeedExtend">
              <el-radio label="0">填充</el-radio>
              <el-radio label="1">不填充</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地区全称" prop="fullName">
            <el-input
              clearable
              v-model="form.fullName"
              placeholder="请输入地区全称"
            />
          </el-form-item>
          <el-form-item label="归属省份" prop="province">
            <el-input
              clearable
              v-model="form.province"
              placeholder="请输入归属省份"
            />
          </el-form-item>
          <el-form-item label="归属市区" prop="city">
            <el-input
              clearable
              v-model="form.city"
              placeholder="请输入归属市区"
            />
          </el-form-item>
          <el-form-item label="归属区县" prop="county">
            <el-input
              clearable
              v-model="form.county"
              placeholder="请输入归属区县"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <div>
        <el-form-item label="备注" prop="remark">
          <el-input
            :rows="4"
            type="textarea"
            v-model="form.remark"
            placeholder="请输入备注"
            style="width: 100%;"
          />
        </el-form-item>
      </div>
      <el-row>

      </el-row>
    </el-form>


    <template #footer>
      <el-button @click="updateModelValue(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import {ref, reactive, watch} from 'vue';
import {EleMessage} from 'ele-admin-plus/es';
import {useFormData} from '@/utils/use-form-data';
import {addPlace, updatePlace} from '@/api/system/place';

const emit = defineEmits(['done', 'update:modelValue']);

const props = defineProps({
  /** 弹窗是否打开 */
  modelValue: Boolean,
  /** 修改回显的数据 */
  data: Object
});

/** 是否是修改 */
const isUpdate = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表单实例 */
const formRef = ref(null);

/** 表单数据 */
const [form, resetFields, assignFields] = useFormData({
  id: void 0,
  name: '',
  code: '',
  remark: '',
  province: '',
  city: '',
  county: '',
  isNeedExtend: '0',
  fullName: '',
});

/** 表单验证规则 */
const rules = reactive({
  name: [
    {required: true, message: '请输入地区名称', trigger: 'blur'},
  ],
  code: [
    {required: true, message: '请输入地区代码', trigger: 'blur'},
  ],
  fullName: [
    {required: true, message: '请输入地区全称', trigger: 'blur'},
  ],
  province: [
    {required: true, message: '请输入归属省份', trigger: 'blur'},
  ],
  // city: [
  //   {required: true, message: '请输入归属市区', trigger: 'blur'},
  // ],
  // county: [
  //   {required: true, message: '请输入归属区县', trigger: 'blur'},
  // ],
  isNeedExtend: [
    {required: true, message: '请选择填充标识', trigger: 'blur'},
  ],
});

/** 保存编辑 */
const save = () => {
  formRef.value?.validate?.((valid) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    const saveOrUpdate = isUpdate.value ? updatePlace : addPlace;
    saveOrUpdate(form)
      .then((msg) => {
        loading.value = false;
        EleMessage.success(msg);
        updateModelValue(false);
        emit('done');
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error(e.message);
      });
  });
};

/** 更新modelValue */
const updateModelValue = (value) => {
  emit('update:modelValue', value);
};

watch(
  () => props.modelValue,
  (modelValue) => {
    if (modelValue) {
      if (props.data) {
        assignFields(props.data);
        isUpdate.value = true;
      } else {
        isUpdate.value = false;
      }
    } else {
      resetFields();
      formRef.value?.clearValidate?.();
    }
  }
);
</script>
